#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
**=============================================================
 * Copyright: 2012~2015 NSN
 * FullName: utils.sql_operate
 * Description: 
 * Changes: 
 *==============================================================
 * Date: Apr 16, 2014
 * Author: y21chen(yong-cy.chen@nsn.com)          
 * Comment:  Create file, implement feature    
**==============================================================
"""
import datetime

from db_conn import GetConnection


class BaseSqlAction(object):
    def __init__(self):
        self.conn = GetConnection()
        
    def add(self,insert_string):
        '''
            Exec sql and then return the add id
        '''
        res = self.conn.exec_sql(insert_string)
        return res
        
    def query(self,query_string):
        '''
            exec query and return the query set
        '''
        res = self.conn.exec_sql(query_string)
        return res
    
    def update(self,update_string):
        '''
            exec the update return update rows
        '''
        res = self.conn.exec_sql(update_string)
        return res
    
    def delete(self,delete_string):
        '''
            exec delete sql return delete rows
        '''
        res = self.conn.exec_sql(delete_string)
        return(res)


if __name__ == "__main__":
    from dict_tosql import DictSqlConvert
    po = BaseSqlAction()
    data = {}
    data['tables'] = "polls_poll"
    
    data['insert'] = {}
    data['insert']['keys_or_values'] = "question,pub_date"
    d = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    data['insert']['dest_or_condition'] = '"test for scripts add 9",""'
    insert_string = DictSqlConvert(data).to_insert()
    
    data['query'] = {}
    data['query']['keys_or_values'] = ''
    data['query']['dest_or_condition'] = 'question="test for scripts add 100"'
    query_string = DictSqlConvert(data).to_query()
    
    data['update'] = {}
    data['update']['keys_or_values'] = 'question="test for scripts add 10",pub_date="'+d+'"'
    data['update']['dest_or_condition'] = 'question="test for scripts add 9"'
    update_string = DictSqlConvert(data).to_update()
     
    data['delete'] = {}
    data['delete']['keys_or_values'] = ''
    data['delete']['dest_or_condition'] = 'question="test for scripts add 4"'
    delete_string = DictSqlConvert(data).to_delete()
    
    po.add(insert_string)


#     po.query(query_string)
# 
# 
#     po.update(update_string)
# 
#     po.delete(delete_string)


    
